package net.oschina.git.hikoyan.helloapi.common;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import net.oschina.git.hikoyan.helloapi.common.request.BaseParam;
import net.oschina.git.hikoyan.helloapi.processor.asyncservlet.AsyncTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by yan on 17/2/13.
 */
@RestController
@Api(value = "base-api", description = "base")
public class BaseController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AsyncTaskService asyncCallService;

    @RequestMapping(value = "/base/{url}", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ApiOperation(value = "base", httpMethod = "POST", notes = "base")
    public DeferredResult<String> base(HttpServletRequest request, HttpServletResponse response,
                                       @ApiParam(required = true, name = "url", value = "真实链接") @PathVariable("url") String url

    ) {
        final DeferredResult<String> deferredResult = new DeferredResult<String>();

        logger.info("/base 调用！thread id is : " + Thread.currentThread().getId());
        logger.info("url:" + url);
        //获取url对应处理配置

        final BaseParam baseParam = new BaseParam();//根据url拼参数
        baseParam.setAppId(request.getParameter("appId"));

//        asyncCallService.makeRemoteCall(new AsyncTaskCallback() {
//            public void callback(Object result) {
//                logger.info("callback 调用！thread id is : " + Thread.currentThread().getId());
//                deferredResult.setResult(result.toString());
//            }
//        }, parameterMap);

        asyncCallService.makeAsyncTask(deferredResult, baseParam);
        return deferredResult;
    }
}
